package client.superadmin;

import static org.stjs.javascript.JSCollections.$array;
import static org.stjs.javascript.JSCollections.$castArray;

import org.stjs.javascript.Array;

import client.common.EndPoints.SupadminEP;
import client.common.EndPoints.UserEP;
import client.common.Models.Colegio;
import client.common.Models.EscalaNotas;
import client.common.Models.Materia;
import client.common.angular.NgScope;
import client.common.angular.UIState;
import client.common.directives.EPLoading;
import client.common.directives.EPModal;
import client.common.javascript.JsConsole;
import client.common.javascript.JsProcedure1;
import enums.Jornada;

public class ColegiosScope extends NgScope {
	private Colegio nuevoColegio;
	private EscalaNotas escalaNotas;
	@SuppressWarnings("unused")
	private Array<Jornada> jornadas;
	private Array<Colegio> colegios;
	private UIState state;
	private SupadminEP superEP;
	private EPModal epModal;
	private EPLoading epLoading;

	public static Array<String> $inject() {
		return $array("Superadmin", "epModal", "User", "$state", "epLoading");
	}

	public ColegiosScope(SupadminEP superEP, EPModal epModal, UserEP userEP,
			UIState state, EPLoading epLoading) {
		super("ColegiosScope");

		this.state = state;
		this.superEP = superEP;
		this.epModal = epModal;
		this.epLoading = epLoading;

		jornadas = $castArray(Jornada.values());
		colegios = superEP.getColegios();

		escalaNotas = new EscalaNotas() {
			{
				min = 0.0;
				basico = 3.0;
				alto = 4.0;
				superior = 4.5;
				max = 5.0;
			}
		};
	}

	public void verColegio(final Colegio c) {
		state.transitionTo("colegio", new Materia() {
			{
				idColegio = c.id;
			}
		});
	}

	public void crearColegio() {
		final ColegiosScope self = this;

		superEP.newColegio(nuevoColegio, new JsProcedure1<Colegio>() {

			@Override
			public void $invoke(Colegio p) {
				self.colegios.push(p);
				JsConsole.console.log("escala notas");
				JsConsole.console.log(self.escalaNotas);
				self.epLoading.$invoke(self.superEP.setEscalaNotas(p.id,
						self.escalaNotas, new JsProcedure1<EscalaNotas>() {

							public void $invoke(EscalaNotas p) {
								self.epModal.close("nuevo-colegio");
								self.nuevoColegio = new Colegio();
								self.escalaNotas = new EscalaNotas() {
									{
										min = 0.0;
										basico = 3.0;
										alto = 4.0;
										superior = 4.5;
										max = 5.0;
									}
								};
							}
						}), "#divNuevoColegio");

			}
		});

	}
}
